package com.health.viewscontrols.medical.device.ue;

import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.GsonUtils;
import com.empsum.blelibrary.bluetoothutils.Data;
import com.empsum.blelibrary.bluetoothutils.StrExchangeBytes;
import com.empsum.blelibrary.bluetoothutils.Utils;
import com.health.httpbean.model.UeModel;
import com.health.viewscontrols.medical.base.BaseDeviceActivity;
import com.nova.health.ble.HexUtil;
import com.nova.health.ble.callback.OnDeviceConnectChangedListener;
import com.novabluetooth.data.DeviceShowSaveData;
import com.novabluetooth.views.BlueToothConstant;

import java.util.UUID;

/**
 * 恩普生 尿液分析仪
 */
public class UEEMPActivity extends BaseDeviceActivity {

    private final UUID UUID_SERVICE = UUID.fromString("000ffe0-0000-1000-8000-00805f9b34fb");
    private final UUID UUID_NOTIFY = UUID.fromString("000ffe1-0000-1000-8000-00805f9b34fb");
    private final UUID UUID_WRITE = UUID.fromString("000ffe1-0000-1000-8000-00805f9b34fb");

    @Override
    public void doBusiness() {
        tv_operation_tips.setText(com.nova.health.ble.R.string.ue_eps);

        mBleDevice.setDevice_Service_UUID(UUID_SERVICE);
        mBleDevice.setDevice_Notify_UUID(UUID_NOTIFY);
        mBleDevice.setDEVICE_write_UUID(UUID_WRITE);
        mBleDevice.setConnectChangedListener(new OnDeviceConnectChangedListener() {
            @Override
            public void onConnected() {
                Log.e(TAG, "onConnected: ");
                isConnected = true;
                runOnUiThread(() -> {
                    blueToothState.setText("蓝牙已连接");
                    showProgressDialog("等待接收数据" + deviceEnum.getTitle() + "数据");
                    new Handler().postDelayed(() -> write(StrExchangeBytes.hexStringToBytes("UIH"))
                            , 1200);
                });
            }

            @Override
            public void onDisconnected() {
                isConnected = false;
                Log.e(TAG, "onDisconnected: ");
                cancelProgressDialog();
                runOnUiThread(() -> {
                    blueToothState.setText("蓝牙连接失败");
                });
            }
        });
        startDevice();
    }

    @Override
    public void startDevice() {
        if (isConnected) {
            blueToothState.setText("蓝牙已连接");
            showProgressDialog("等待接收数据" + deviceEnum.getTitle() + "数据");
        } else {
            showProgressDialog("正在连接" + deviceEnum.getTitle() + "设备");
            blueToothState.setText("蓝牙开始连接");
            Log.e(TAG, "startDevice: " + deviceEnum.getMac());
            mBleDevice.setConnect(true);
            mBleDevice.setMAC(deviceEnum.getMac());
            mBleDevice.connect(deviceEnum.getMac());
        }
    }

    @Override
    public void update(String hexStr) {
        Log.e("xiaoxin", "onDataCallBack: " + hexStr);
        Data data = Utils.parserOneData(HexUtil.hexStringToBytes(hexStr));
        if (data != null) {
            String ubg = data.getUBGValue() + "(" + data.getUBG() + ")";
            String bld = data.getBLDValue() + "(" + data.getBLD() + ")";
            String bil = data.getBILValue() + "(" + data.getBIL() + ")";
            String ket = data.getKETValue() + "(" + data.getKET() + ")";
            String glu = data.getGLUValue() + "(" + data.getGLU() + ")";
            String pro = data.getPROValue() + "(" + data.getPRO() + ")";
            String ph = data.getPH();
            String nit = data.getNITValue() + "(" + data.getNIT() + ")";
            String leu = data.getLEUValue() + "(" + data.getLEU() + ")";
            String sg = data.getSG();
            String vc = data.getVCValue() + "(" + data.getVC() + ")";
            DeviceShowSaveData.getNiaoYeBLE_EMP_UI(ubg, bld, bil, ket, glu, pro, ph, nit, leu, sg, vc);


            // 保存数据到新表 上传的新平台使用
            UeModel ueModel = new UeModel();
            ueModel.setUro(ubg);
            ueModel.setBld(bld);
            ueModel.setBil(bil);
            ueModel.setKet(ket);
            ueModel.setGlu(glu);
            ueModel.setPro(pro);
            ueModel.setPh(ph);
            ueModel.setNit(nit);
            ueModel.setLeu(leu);
            ueModel.setSg(sg);
            ueModel.setVc(vc);

            DeviceShowSaveData.saveUE(GsonUtils.toJson(ueModel));

            final StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("测量结果:\n"
                    + "尿胆原:" + ubg
                    + "  潜血:" + bld
                    + "  胆红素:" + bil
                    + "  酮体:" + ket
                    + "  葡萄糖:" + glu
                    + "  蛋白质:" + pro
                    + "  PH:" + ph
                    + "  亚硝酸盐:" + nit
                    + "  白细胞:" + leu
                    + "  比重:" + sg
                    + "  维生素:" + vc
            );
            runOnUiThread(() -> {
                Log.e(TAG, "run: " + stringBuilder.toString());
                tv_state.setText("");
                tv_result.setText(stringBuilder.toString());
                disconn();
            });
        }
    }


    @Override
    public void disconnectDevice() {
        disconn();
    }

    private void disconn() {
        try {
            cancelProgressDialog();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            isConnected = false;
            mBleDevice.setConnect(false);
            mBleDevice.disconnect();
            mBleDevice.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
